package UILayer;
import ModelLayer.*;
import CtrlLayer.*;
import java.util.*;

public class ContractorsUI
{
    ContractorController contractorController;
    public ContractorsUI()
    {
        contractorController = new ContractorController();
    }

    public void contractorsMenuStart(int sessionId)
    {
        boolean exit=false;
        while (!exit) 
        {
            int choice = writeContractorsMenu(sessionId);
            if(SessionContainer.getInstance().findSessionById(sessionId).getAccessLevel() == 2) {
                switch(choice){
                    case 1:
                    startCreateContractor();
                    break;
                    case 2:
                    startFindContractor();
                    break;
                    case 3:
                    startUpdateContractor();
                    break;
                    case 4:
                    startDeleteContractor();
                    break;
                    case 0:
                    exit = true;
                    break;

                }
            } else {
                switch(choice){
                    case 1:
                    startCreateContractor();
                    break;
                    case 2:
                    startFindContractor();
                    break;
                    case 3:
                    startUpdateContractor();
                    break;
                    case 0:
                    exit = true;
                    break;
                }
            }
        }
    }

    private int writeContractorsMenu(int sessionId)
    {       

        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f *** Contractors Menu ***");
        System.out.println(" (1) Add contractor");
        System.out.println(" (2) Show contractor");
        System.out.println(" (3) Update contractor  ");
        if(  SessionContainer.getInstance().findSessionById(sessionId).getAccessLevel() == 2 ){
            System.out.println(" (4) Delete contractor"); 
        }
        System.out.println(" (0) Back");
        System.out.print("\n Make your choice: ");
        int choice = keyboard.nextInt();
        return choice;
    }

    public void startCreateContractor()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the information of the new contractor:");
        System.out.println("ID:");
        int id = Integer.parseInt(keyboard.nextLine());
        System.out.println("Name:");
        String name = keyboard.nextLine();
        System.out.println("E-mail:");
        String email = keyboard.nextLine();
        System.out.println("Phone number: ");
        String phoneNumber = keyboard.nextLine();
        System.out.println("Bank account: ");
        String bankAccount = keyboard.nextLine();
        
        if(contractorController.createContractor(id,name,email,phoneNumber,bankAccount)) {
            System.out.println("Contractor with id " + id + " has been added!");
        } else {
            System.out.println("Contractor with id " + id + " already exists!");
        }
        
        pause();
    }   

    public void startFindContractor()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("ID:");
        int id = Integer.parseInt(keyboard.nextLine());
        if(contractorController.findContractor(id) != null) {
            System.out.println("The contractor has been found!");
        } else {
            System.out.println("The contractor has not been found!");
        }
        pause();
    }

    public void startUpdateContractor()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the ID of the contractor you want to update:");
        int id = Integer.parseInt(keyboard.nextLine());
        System.out.println("Name:");
        String name = keyboard.nextLine();
        System.out.println("E-mail:");
        String email = keyboard.nextLine();
        System.out.println("Phone number: ");
        String phoneNumber = keyboard.nextLine();
        System.out.println("Bank account: ");
        String bankAccount = keyboard.nextLine();
        contractorController.updateContractor(id,name,email,phoneNumber,bankAccount);
        pause();
    }

    public void startDeleteContractor()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Enter the ID of the contractor you want to delete:");
        int id = Integer.parseInt(keyboard.nextLine());
        contractorController.deleteContractor(id);
        pause();
    }

    private void pause()
    {
        Scanner keyboard = new Scanner(System.in);
        String vent = keyboard.nextLine();
    }

}
